import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { DatePipe } from '@angular/common';

/**
 * 首页
 */

@Component({
    templateUrl: './logMgmt.html',
})
export class LogMgmtComponent implements OnInit {
    pages = {
        pageNo: 1,
        pageSize: 10,
        content: "",
        beginTime: "",
        endTime: "",
        logType: ""
    };
    dataList = [];
    count = {};
    tableLoading = false;
    ngOnInit() {
        this.loadList();
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService,
        private datePipe: DatePipe
    ) { }
    isSelectedDate = (date) => {
        const value = parseInt(this.datePipe.transform(date, 'yyyyMMdd'));
        const today = parseInt(this.datePipe.transform(new Date(), 'yyyyMMdd'));
        return value > today;
    };
    loadList() {
        this.tableLoading = true;
        this.apiService.post('日志列表', this.pages).then((res: any) => {
            this.dataList = res.data.list;
        }).finally(() => {
            this.tableLoading = false;
        })
    }
    changeFilter() {
        this.pages.pageNo = 1;
        this.loadList();
    }
    getTypeName(type) {
        const obj = {
            Device: '设备类', Interface: '接口类', Permission: '权限类', Collection: '采集类'
        };
        return obj[type] || '-';
    }
    onChange(value) {
        if (value && value.length === 2) {
            this.pages.beginTime = this.datePipe.transform(value[0], 'yyyy-MM-dd');
            this.pages.endTime = this.datePipe.transform(value[1], 'yyyy-MM-dd');
            this.changeFilter();
        } else {
            this.pages.beginTime = '';
            this.pages.endTime = '';
            this.changeFilter();
        }
    }
}
